2 # Blocks and bans object
4 #TODO: This could be used everywhere, but it isn't.
6 # All the functions in this class assume the object is either explicitly
7 # loaded or filled. It is not load-on-demand. There are no accessors.
9 # To use delete(), you only need to fill $mAddress
11 # Globals used: $wgBlockCache, $wgAutoblockExpiry
13 define ( 'EB_KEEP_EXPIRED', 1 );
14 define ( 'EB_FOR_UPDATE', 2 );
18 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
19 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
21 function Block( $address = '', $user = '', $by = 0, $reason = '',
22 $timestamp = '' , $auto = 0, $expiry = '' )
24 $this->mAddress
= $address;
27 $this->mReason
= $reason;
28 $this->mTimestamp
= $timestamp;
30 $this->mExpiry
= $expiry;
32 $this->mForUpdate
= false;
33 $this->initialiseRange();
36 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
39 $ban->load( $address, $user, $killExpired );
45 $mAddress = $mReason = $mTimestamp = '';
49 # Get a ban from the DB, with either the given address or the given username
50 function load( $address = "", $user = 0, $killExpired = true )
52 $fname = 'Block::load';
56 if ( $this->forUpdate() ) {
57 $db =& wfGetDB( DB_MASTER
);
58 $options = 'FOR UPDATE';
60 $db =& wfGetDB( DB_SLAVE
);
63 $ipblocks = $db->tableName( 'ipblocks' );
65 if ( 0 == $user && $address=="" ) {
66 $sql = "SELECT * from $ipblocks $options";
67 } elseif ($address=="") {
68 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
69 } elseif ($user=="") {
70 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
72 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
73 "' OR ipb_user={$user}) $options";
76 $res = $db->query( $sql, $fname );
77 if ( 0 == $db->numRows( $res ) ) {
82 $row = $db->fetchObject( $res );
83 $this->initFromRow( $row );
86 # If requested, delete expired rows
88 $killed = $this->deleteIfExpired();
90 $row = $db->fetchObject( $res );
92 $this->initFromRow( $row );
95 } while ( $killed && $row );
97 # If there were any left after the killing finished, return true
108 $db->freeResult( $res );
112 function initFromRow( $row )
114 $this->mAddress
= $row->ipb_address
;
115 $this->mReason
= $row->ipb_reason
;
116 $this->mTimestamp
= $row->ipb_timestamp
;
117 $this->mUser
= $row->ipb_user
;
118 $this->mBy
= $row->ipb_by
;
119 $this->mAuto
= $row->ipb_auto
;
120 $this->mId
= $row->ipb_id
;
121 $this->mExpiry
= $row->ipb_expiry
;
123 $this->initialiseRange();
126 function initialiseRange()
128 if ( $this->mUser
== 0 ) {
129 $rangeParts = explode( '/', $this->mAddress
);
130 if ( count( $rangeParts ) == 2 ) {
131 $this->mNetworkBits
= $rangeParts[1];
133 $this->mNetworkBits
= 32;
135 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
137 $this->mNetworkBits
= false;
138 $this->mIntegerAddr
= false;
142 # Callback with a Block object for every block
143 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
145 $block = new Block();
146 if ( $flags & EB_FOR_UPDATE
) {
147 $db =& wfGetDB( DB_MASTER
);
148 $options = 'FOR UPDATE';
149 $block->forUpdate( true );
151 $db =& wfGetDB( DB_SLAVE
);
154 $ipblocks = $db->tableName( 'ipblocks' );
156 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
157 $res = $db->query( $sql, 'Block::enumBans' );
159 while ( $row = $db->fetchObject( $res ) ) {
160 $block->initFromRow( $row );
161 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
162 if ( !$block->deleteIfExpired() ) {
163 $callback( $block, $tag );
166 $callback( $block, $tag );
169 wfFreeResult( $res );
174 $fname = 'Block::delete';
175 $dbw =& wfGetDB( DB_MASTER
);
177 if ( $this->mAddress
== "" ) {
178 $condition = array( 'ipb_id' => $this->mId
);
180 $condition = array( 'ipb_address' => $this->mAddress
);
182 $dbw->delete( 'ipblocks', $condition, $fname );
188 $dbw =& wfGetDB( DB_MASTER
);
189 $dbw->insertArray( 'ipblocks',
191 'ipb_address' => $this->mAddress
,
192 'ipb_user' => $this->mUser
,
193 'ipb_by' => $this->mBy
,
194 'ipb_reason' => $this->mReason
,
195 'ipb_timestamp' => $this->mTimestamp
,
196 'ipb_auto' => $this->mAuto
,
197 'ipb_expiry' => $this->mExpiry
,
204 function deleteIfExpired()
206 if ( $this->isExpired() ) {
216 if ( !$this->mExpiry
) {
219 return wfTimestampNow() > $this->mExpiry
;
225 return $this->mAddress
!= '';
228 function updateTimestamp()
230 if ( $this->mAuto
) {
231 $this->mTimestamp
= wfTimestampNow();
232 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
234 $dbw =& wfGetDB( DB_MASTER
);
235 $dbw->updateArray( 'ipblocks',
237 'ipb_timestamp' => $this->mTimestamp
,
238 'ipb_expiry' => $this->mExpiry
,
239 ), array( /* WHERE */
240 'ipb_address' => $this->mAddress
241 ), 'Block::updateTimestamp'
248 /* private */ function clearCache()
250 global $wgBlockCache;
251 if ( is_object( $wgBlockCache ) ) {
252 $wgBlockCache->loadFromDB();
256 function getIntegerAddr()
258 return $this->mIntegerAddr
;
261 function getNetworkBits()
263 return $this->mNetworkBits
;
266 function forUpdate( $x = NULL ) {
267 return wfSetVar( $this->mForUpdate
, $x );
270 /* static */ function getAutoblockExpiry( $timestamp )
272 global $wgAutoblockExpiry;
273 return wfUnix2Timestamp( wfTimestamp2Unix( $timestamp ) +
$wgAutoblockExpiry );
276 /* static */ function normaliseRange( $range )
278 $parts = explode( '/', $range );
279 if ( count( $parts ) == 2 ) {
280 $shift = 32 - $parts[1];
281 $ipint = ip2long( $parts[0] );
282 $ipint = $ipint >> $shift << $shift;
283 $newip = long2ip( $ipint );
284 $range = "$newip/{$parts[1]}";